﻿singleApp.controller('loginCtrl', ['$http', '$scope', function ($http, $scope) {
	$scope.greeting = 'Hola!';
	$scope.vm = {
	    phone: "",
		password: "",
	};

    var InterValObj;
    var curCount;
    var count=60;
    var curBollean=true;
	$scope.getsms = function () {
        curCount=count;
		console.log();
        if ($scope.vm.phone == undefined || $scope.vm.phone == "") {
            layer.msg("请输入11位手机号 !");
            return;
        }
		if(curBollean=true){
            var api_url = window.singleConfig.rootUrl + 'api/user/AuthService/smsSend';
            layer.load(2);
            $http({
                method: 'POST',
                url: api_url,
                data: $.param($scope.vm),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            })
                .then(function (result) {
                    var data = result.data;
                    if (data.code == "success") {
                        layer.closeAll();
                        layer.msg("发送成功!");
                    }
                    else {
                        layer.closeAll();
                        layer.msg(data.message);
                    }
                },function (data) {
                    layer.msg("发送失败!");
                });
            document.getElementById("btnSmsSend").setAttribute("class","button button-round disabled" );//设置按钮为禁用状
            curBollean=false;
            document.getElementById("btnSmsSend").innerText=curCount + "秒后再次获取";
            InterValObj = window.setInterval(SetRemainTime, 1000); // 启动计时器timer处理函数，1秒执行一次
		}
		else{
            document.getElementById("btnSmsSend").innerText=curCount + "秒后再次获取";
		}

    }

    //timer处理函数

    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);// 停止计时器
            document.getElementById("btnSmsSend").setAttribute("class","button button-round button-danger" );//移除禁用状态改为可用
            curBollean=true;
            document.getElementById("btnSmsSend").innerText="重新获取验证码";
        }else {
            curCount--;
            curBollean=false;
            document.getElementById("btnSmsSend").innerText=curCount + "秒后再次获取";
        }
    }


	$scope.submit = function () {
	    if ($scope.vm.phone == undefined || $scope.vm.phone == "") {
			layer.msg("请输入11位手机号 !");
			return;
		}
		if ($scope.vm.password == undefined || $scope.vm.password == "") {
			layer.msg("请输入密码 !");
			return;
		}

		var api_url = window.singleConfig.rootUrl + 'api/user/AuthService/loginPost';
		layer.load(2);
		$http({
			method: 'POST',
			url: api_url,
			data: $.param($scope.vm),
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
		})
		.then(function (result) {
			var data = result.data;
			if (data.code == "success") {
				layer.closeAll();
				if (window.singleConfig.redirectUrl != undefined && window.singleConfig.redirectUrl != "") {
				    location.href = window.singleConfig.redirectUrl;
				}
				else {
				    location.href = data.redirectUrl;
				}
			}
			else {
				layer.closeAll();		    
				layer.msg(data.message);
			}
		},function (data) {		
			layer.msg("手机号或者密码错误!");
		});	
	}


}]);